package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserParam;
import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.domain.TravelComment;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.query.TravelQuery;
import cn.wolfcode.luowowo.redis.RedisService.IUserInfoRedisService;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.ITravelCommentService;
import cn.wolfcode.luowowo.service.ITravelService;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.util.UMEditorUploader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.jws.Oneway;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 游记控制器
 */
@RestController
@RequestMapping("/travels")
public class TravelController {
    //注入游记服务接口
    @Autowired
    private ITravelService travelService;
    //注入目的地接口
    @Autowired
    private IDestinationService destinationService;
    //注入用户服务接口
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private ITravelCommentService travelCommentService;

    //分页查询
    @GetMapping("/query")
    public Object query(TravelQuery qo) {
        Page<Travel> page = travelService.query(qo);
        return JsonResult.success(page);
    }

    //获取游记编辑需要的数据
    @GetMapping("/input")
    public Object input(String id) {
        //先创建一个空的map
        Map<String, Object> map = new HashMap();
        //判断是否有id，如果有就是编辑操作，没有就是保存操作，直接跳过
        if (StringUtils.hasLength(id)) {
            Travel travel = travelService.get(id);
            map.put("tv", travel);
        }
        //查询目的地
        List<Destination> list = destinationService.listAll();
        //将目的地添加到map集合中返回到页面
        map.put("dests", list);
        return JsonResult.success(map);
    }

    //上传图片
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception {
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif", ".png", ".jpg", ".jpeg", ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\"" + up.getFileName() + "\", \"originalName\": \"" + up.getOriginalName() + "\", \"size\": " + up.getSize()
                + ", \"state\": \"" + up.getState() + "\", \"type\": \"" + up.getType() + "\", \"url\": \"" + up.getUrl() + "\"}";
        result = result.replaceAll("\\\\", "\\\\");
        if (callback == null) {
            return result;
        } else {
            return "<script>" + callback + "(" + result + ")</script>";
        }
    }

    @RequireLogin//登录才能保存
    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(Travel travel, HttpServletRequest request) {
        String token = request.getHeader("token");
        //通过令牌存的用户id获取用户信息
        UserInfo user = userInfoRedisService.getUserByToken(token);
        travel.setUserId(user.getId());
        travelService.saveOrUpdate(travel);
        return JsonResult.success(travel.getId());
    }

    //明细
    @GetMapping("/detail")
    public Object detail(String id) {
        return JsonResult.success(travelService.get(id));
    }


    //游记的评论添加
    @RequireLogin
    @PostMapping("/commentAdd")
    public Object commentAdd(TravelComment comment, @UserParam UserInfo userInfo) {

        //用户游记评论
        BeanUtils.copyProperties(userInfo, comment);
        comment.setUserId(userInfo.getId());
        travelCommentService.addComment(comment);

        return JsonResult.success();
    }

    @GetMapping("/comments")
    public Object comments(String travelId) {
        return JsonResult.success(travelCommentService.queryByTravelId(travelId));
    }
}
